package cn.schoolwow.workflow.module.definition.action.service;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.transaction.Transaction;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceQuery;
import cn.schoolwow.workflow.entity.WorkFlowDefinition;
import cn.schoolwow.workflow.entity.WorkFlowNode;
import cn.schoolwow.workflow.entity.WorkFlowNodeJump;
import cn.schoolwow.workflow.module.instance.action.service.DeleteWorkFlowInstanceServiceFlow;

public class DeleteWorkFlowDefinitionServiceFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setWorkFlowDefinitionId(flowContext);
        deleteTransaction(flowContext);
    }

    @Override
    public String name() {
        return "删除工作流定义服务流程";
    }

    private void setWorkFlowDefinitionId(FlowContext flowContext){
        String name = flowContext.checkData("name", String.class);
        DAO dao = flowContext.checkInstanceData(DAO.class);

        Long workFlowDefinitionId = dao.query(WorkFlowDefinition.class)
                .addQuery("name", name)
                .addColumn("id")
                .execute()
                .getSingleColumn(Long.class);
        if(null==workFlowDefinitionId){
            flowContext.brokenCurrentFlow("工作流定义不存在!名称:"+name);
        }
        flowContext.putCurrentFlowData("workFlowDefinitionId", workFlowDefinitionId);
    }

    private void deleteTransaction(FlowContext flowContext){
        Long workFlowDefinitionId = flowContext.checkData("workFlowDefinitionId", Long.class);
        DAO dao = flowContext.checkInstanceData(DAO.class);

        WorkFlowInstanceQuery workFlowInstanceQuery = new WorkFlowInstanceQuery();
        workFlowInstanceQuery.definitionIdList.add(workFlowDefinitionId);

        dao.startTransaction(transaction -> {
            flowContext.putTemporaryInstanceData(transaction, Transaction.class);
            deleteWorkFlowDefinition(flowContext);
            deleteWorkFlowNode(flowContext);
            deleteWorkFlowJump(flowContext);
            //删除工作流
            flowContext.startFlow(new DeleteWorkFlowInstanceServiceFlow())
                    .putCurrentCompositeFlowInstanceData(workFlowInstanceQuery)
                    .execute();
        });
    }

    private void deleteWorkFlowDefinition(FlowContext flowContext){
        Long workFlowDefinitionId = flowContext.checkData("workFlowDefinitionId", Long.class);
        Transaction transaction = flowContext.checkInstanceData(Transaction.class);

        transaction.addRecordString("删除工作流定义信息");
        transaction.query(WorkFlowDefinition.class)
                .addQuery("id", workFlowDefinitionId)
                .execute()
                .delete();
    }

    private void deleteWorkFlowNode(FlowContext flowContext){
        Long workFlowDefinitionId = flowContext.checkData("workFlowDefinitionId", Long.class);
        Transaction transaction = flowContext.checkInstanceData(Transaction.class);

        transaction.addRecordString("删除工作流节点信息");
        transaction.query(WorkFlowNode.class)
                .addQuery("definitionId", workFlowDefinitionId)
                .execute()
                .delete();
    }

    private void deleteWorkFlowJump(FlowContext flowContext){
        Long workFlowDefinitionId = flowContext.checkData("workFlowDefinitionId", Long.class);
        Transaction transaction = flowContext.checkInstanceData(Transaction.class);

        transaction.addRecordString("删除工作流节点跳转信息");
        transaction.query(WorkFlowNodeJump.class)
                .addQuery("definitionId", workFlowDefinitionId)
                .execute()
                .delete();
    }

}
